package com.lepu.app.fun.mother_circle.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.mother_circle.MotherTopicActivity;
import com.lepu.app.fun.mother_circle.bean.BeanMotherTopic;
import com.lepu.app.fun.my.PubContentDetailActivity;
import com.lepu.app.fun.my.bean.BeanPubContentImage;
import com.lepu.app.main.activity.AppImageBroswerActivity;

public class AdapterMotherTopicShow extends BaseAdapter implements AsyncRequest{

	private String REQUEST_LIKE = "request_like";
	private String REQUEST_LIKE_CANNEL = "request_like_cannel";
	private static final int MSG_LIKE_SUCESS = 11;
	private static final int MSG_LIKE_FAIL = 12;
	private static final int MSG_LIKE_CANNEL_SUCESS = 13;
	private static final int MSG_LIKE_CANNEL_FAIL = 14;
	
	private LayoutInflater mLayoutInflater = null;
	public Context mContext = null;
	public ArrayList<BeanMotherTopic> mArrayList = null;
	public int mTopicType = 1; //1 代表全部   2代表最新  3代表精品
	
	public int mPosition;
	public TextView mTextView;

	public AdapterMotherTopicShow(Context context,ArrayList<BeanMotherTopic> arrayList, int topicType) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mTopicType = topicType;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.mother_topic_show_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView.findViewById(R.id.mainItemLayout);
			CircularImageView iconImageView = (CircularImageView) convertView.findViewById(R.id.iconImageView);
			
			TextView userNameTextView = (TextView) convertView.findViewById(R.id.topic_item_name);
			TextView timeTextView = (TextView) convertView.findViewById(R.id.topic_item_time);
			TextView titleTextView = (TextView) convertView.findViewById(R.id.topic_item_title);
			TextView contentTextView = (TextView) convertView.findViewById(R.id.topic_item_content);
			ImageView goodImageView = (ImageView) convertView.findViewById(R.id.topic_item_good_icon);
			ImageView imgImageView = (ImageView) convertView.findViewById(R.id.topic_item_img_icon);
			
			TextView commentCountTextView = (TextView) convertView.findViewById(R.id.topic_item_comment);
			TextView lookCountTextView = (TextView) convertView.findViewById(R.id.topic_item_look);
			TextView likeTextView = (TextView) convertView.findViewById(R.id.topic_item_like);
			ImageButton replyButton = (ImageButton) convertView.findViewById(R.id.topic_item_reply);
			
			ImageView commentImage11 = (ImageView) convertView.findViewById(R.id.comment_image_11);
			ImageView commentImage21 = (ImageView) convertView.findViewById(R.id.comment_image_21);
			ImageView commentImage22 = (ImageView) convertView.findViewById(R.id.comment_image_22);
			ImageView commentImage31 = (ImageView) convertView.findViewById(R.id.comment_image_31);
			ImageView commentImage32 = (ImageView) convertView.findViewById(R.id.comment_image_32);
			ImageView commentImage33 = (ImageView) convertView.findViewById(R.id.comment_image_33);
			LinearLayout commentImageLayout = (LinearLayout) convertView.findViewById(R.id.comment_image_layout);
			LinearLayout commentImageLayout1 = (LinearLayout) convertView.findViewById(R.id.comment_image_layout1);
			LinearLayout commentImageLayout2 = (LinearLayout) convertView.findViewById(R.id.comment_image_layout2);
			LinearLayout commentImageLayout3 = (LinearLayout) convertView.findViewById(R.id.comment_image_layout3);
			
			holder.mainItemLayout = mainItemLayout;
			holder.iconImageView = iconImageView;
			holder.userNameTextView = userNameTextView;
			holder.timeTextView = timeTextView;
			holder.titleTextView = titleTextView;
			holder.contentTextView = contentTextView;
			holder.goodImageView = goodImageView;
			holder.imgImageView = imgImageView;
			holder.lookCountTextView = lookCountTextView;
			holder.commentCountTextView = commentCountTextView;
			holder.likeTextView = likeTextView;
			holder.replyButton = replyButton;
			
			holder.commentImage11 = commentImage11;
			holder.commentImage21 = commentImage21;
			holder.commentImage22 = commentImage22;
			holder.commentImage31 = commentImage31;
			holder.commentImage32 = commentImage32;
			holder.commentImage33 = commentImage33;
			holder.commentImageLayout = commentImageLayout;
			holder.commentImageLayout1 = commentImageLayout1;
			holder.commentImageLayout2 = commentImageLayout2;
			holder.commentImageLayout3 = commentImageLayout3;
            			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.mainItemLayout.setTag(position);

		final BeanMotherTopic item = mArrayList.get(position);

		String imageUrl = Setting.getImageUrl(item.topicAvatar);
		//UniversalImageLoadTool.disPlay(imageUrl, holder.iconImageView, R.drawable.app_ic_default_avatar_64);
		LoginUtil.loadSelfUserImage(mContext, holder.iconImageView, imageUrl);

		holder.userNameTextView.setText(item.topicUserName);
		
		holder.commentCountTextView.setText(item.topicCommentCount);
		holder.lookCountTextView.setText(item.topicLookCount);
		
		//time
		if(mTopicType == 1){
			holder.timeTextView.setText(DateUtilBase.friendly_time(item.topicReplyTime));
		}else{
			holder.timeTextView.setText(DateUtilBase.friendly_time(item.topicTime));
		}
		
		
		boolean hasDate = false;
		for (int i = 0; i < item.MediaList.size(); i++) {
			if (!TextUtils.isEmpty(item.MediaList.get(i).MediaUri)) {
				hasDate = true;
			}
		}

		if(item.topicIsGood == 1){
			holder.goodImageView.setVisibility(View.VISIBLE);
			if(hasDate){
				holder.imgImageView.setVisibility(View.VISIBLE);
				holder.titleTextView.setText("            "+item.topicTitle);
			}else{
				holder.imgImageView.setVisibility(View.GONE);
				holder.titleTextView.setText("      "+item.topicTitle);
			}
			
		}else{
			holder.goodImageView.setVisibility(View.GONE);
			if(hasDate){
				holder.imgImageView.setVisibility(View.VISIBLE);
				holder.titleTextView.setText("      "+item.topicTitle);
			}else{
				holder.imgImageView.setVisibility(View.GONE);
				holder.titleTextView.setText(""+item.topicTitle);
			}
			
		}
		UIHelper.setTextViewLines(holder.titleTextView, 2);
		
		holder.contentTextView.setText(item.topicContent);
		UIHelper.setTextViewLines(holder.contentTextView, 2);
		
        //内容 隐藏
        holder.contentTextView.setVisibility(View.GONE);
		
		// 图片
		if (item.MediaList != null && item.MediaList.size() > 0) {

			if (hasDate) {
				holder.commentImageLayout.setVisibility(View.VISIBLE);
			} else {
				holder.commentImageLayout.setVisibility(View.GONE);
			}

			if (item.MediaList.size() == 1) {
				holder.commentImageLayout1.setVisibility(View.VISIBLE);
				holder.commentImageLayout2.setVisibility(View.GONE);
				holder.commentImageLayout3.setVisibility(View.GONE);
				UniversalImageLoadTool.disPlay(getImageUrl(item.MediaList.get(0).MediaUri),holder.commentImage11,R.drawable.default_icon_big);
				holder.commentImage11.setOnClickListener(imageClickListener(item.MediaList, 1));
			} else if (item.MediaList.size() == 2) {
				holder.commentImageLayout2.setVisibility(View.VISIBLE);
				holder.commentImageLayout1.setVisibility(View.GONE);
				holder.commentImageLayout3.setVisibility(View.GONE);
				UniversalImageLoadTool.disPlay(getImageUrl(item.MediaList.get(0).MediaUri),holder.commentImage21,R.drawable.default_icon_big);
				UniversalImageLoadTool.disPlay(getImageUrl(item.MediaList.get(1).MediaUri),holder.commentImage22,R.drawable.default_icon_big);
				holder.commentImage21.setOnClickListener(imageClickListener(item.MediaList, 1));
				holder.commentImage22.setOnClickListener(imageClickListener(item.MediaList, 2));
			} else if (item.MediaList.size() == 3) {
				holder.commentImageLayout3.setVisibility(View.VISIBLE);
				holder.commentImageLayout1.setVisibility(View.GONE);
				holder.commentImageLayout2.setVisibility(View.GONE);
				UniversalImageLoadTool.disPlay(getImageUrl(item.MediaList.get(0).MediaUri),holder.commentImage31,R.drawable.default_icon_big);
				UniversalImageLoadTool.disPlay(getImageUrl(item.MediaList.get(1).MediaUri),holder.commentImage32,R.drawable.default_icon_big);
				UniversalImageLoadTool.disPlay(getImageUrl(item.MediaList.get(2).MediaUri),holder.commentImage33,R.drawable.default_icon_big);
				holder.commentImage31.setOnClickListener(imageClickListener(item.MediaList, 1));
				holder.commentImage32.setOnClickListener(imageClickListener(item.MediaList, 2));
				holder.commentImage33.setOnClickListener(imageClickListener(item.MediaList, 3));
			} else {

			}
		} else {
			holder.commentImageLayout.setVisibility(View.GONE);
		}
		
		//赞
		holder.likeTextView.setText(item.topicLikeCount+" 点赞 ");
		if(item.topicIsLike == 1){
			Drawable drawable = mContext.getResources().getDrawable( R.drawable.mother_topic_show_like_yes);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			holder.likeTextView.setCompoundDrawables(null, null, drawable, null);
		}else{
			Drawable drawable = mContext.getResources().getDrawable( R.drawable.mother_topic_show_like_no);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			holder.likeTextView.setCompoundDrawables(null, null, drawable, null);
		}
		
		holder.likeTextView.setOnClickListener(new ViewOnclicklistener(position, holder.likeTextView));
		
		//回复
		holder.replyButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,PubContentDetailActivity.class);
				intent.putExtra("tag", 0);
				intent.putExtra("TopicId", item.topicID);
				((MotherTopicActivity)mContext).startActivity(intent, true);
			}
		});

		holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,PubContentDetailActivity.class);
				intent.putExtra("tag", 0);
				intent.putExtra("TopicId", item.topicID);
				((MotherTopicActivity)mContext).startActivity(intent, true);
			}
		});

		return convertView;
	}

	class ViewOnclicklistener implements View.OnClickListener {
		private int position;
		private TextView imageView;
		public ViewOnclicklistener(int position, TextView imageView){
			this.position = position;
			this.imageView = imageView;
		}
		@Override
		public void onClick(View v) {
			int rid = v.getId();
			switch (rid) {
			case R.id.topic_item_like:
				if(LoginUtil.jumpLogin((BaseFragmentActivity)mContext)){
					return;
				}
				mPosition = position;
				mTextView = imageView;
				if(mArrayList.get(mPosition).topicIsLike == 1){
					requestCollect(0);
				}else{
					requestCollect(1);
				}
				
				break;
			}
		}
	}
	
	// 图片监听
	private View.OnClickListener imageClickListener(final ArrayList<BeanPubContentImage> mArrayList,final int position) {
		return new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String[] urlArray = new String[mArrayList.size()];

				for (int i = 0; i < mArrayList.size(); i++) {
					urlArray[i] = mArrayList.get(i).MediaUri;
				}
				
				Intent intent = new Intent(mContext,AppImageBroswerActivity.class);
				intent.putExtra("isShowTopbar", false);
				intent.putExtra("index", position - 1);
				intent.putExtra("urlArray", urlArray);
				((BaseFragmentActivity) mContext).startActivity(intent);
			}
		};
	}
	
	public String getImageUrl(String url){
		String imageUrl = "";
		if(url.startsWith("http://"))
		{
			imageUrl = url;
		}else{
			imageUrl = Setting.getImageUrl(url);
		}
		
		return imageUrl;
	}
	
	public void requestCollect(int like) {
		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			
			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			params.put("TopicID", mArrayList.get(mPosition).topicID);

		} catch (Exception e) {
		}
		map.put("ht", params);

		if(like == 1){
			ApiClient.http_post(Setting.getFavourTopicUrl(), map, null, this,REQUEST_LIKE, true, false);
		}else{
			ApiClient.http_post(Setting.getCancelFavourTopicUrl(), map, null, this,REQUEST_LIKE_CANNEL, true, false);
		}
		
	}
	
	@SuppressLint("HandlerLeak")
	public Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LIKE_SUCESS:
				mArrayList.get(mPosition).topicIsLike = 1;
				mArrayList.get(mPosition).topicLikeCount = String.valueOf(Integer.valueOf(mArrayList.get(mPosition).topicLikeCount) + 1);
				mTextView.setText(mArrayList.get(mPosition).topicLikeCount+" 点赞 ");
				
				Drawable drawable1 = mContext.getResources().getDrawable( R.drawable.mother_topic_show_like_yes);
				drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
				mTextView.setCompoundDrawables(null, null, drawable1, null);

				break;

			case MSG_LIKE_FAIL:
				
				break;
			case MSG_LIKE_CANNEL_SUCESS:
				mArrayList.get(mPosition).topicIsLike = 0;
				
				mArrayList.get(mPosition).topicLikeCount = String.valueOf(Integer.valueOf(mArrayList.get(mPosition).topicLikeCount) - 1);
				mTextView.setText(mArrayList.get(mPosition).topicLikeCount+" 点赞 ");
				
				Drawable drawable2 = mContext.getResources().getDrawable( R.drawable.mother_topic_show_like_no);
				drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
				mTextView.setCompoundDrawables(null, null, drawable2, null);
				break;
			case MSG_LIKE_CANNEL_FAIL:
				break;
			default:
				break;
			}
		};
	};

	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_LIKE)) {
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_LIKE_SUCESS;
			msg.obj = (String) data;
			mHandler.sendMessage(msg);
		}else if(object.equals(REQUEST_LIKE_CANNEL)){
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_LIKE_CANNEL_SUCESS;
			msg.obj = (String) data;
			mHandler.sendMessage(msg);
		}
		
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		if (object.equals(REQUEST_LIKE)) {
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_LIKE_FAIL;
			mHandler.sendMessage(msg);
		}else if(object.equals(REQUEST_LIKE_CANNEL)){
			Message msg = mHandler.obtainMessage();
			msg.what = MSG_LIKE_CANNEL_FAIL;
			mHandler.sendMessage(msg);
		}
		
	}
	
	private static class ViewHolder {

		LinearLayout mainItemLayout;
		CircularImageView iconImageView;
		
		TextView userNameTextView;
		TextView timeTextView;
		TextView titleTextView;
		TextView contentTextView;
		ImageView goodImageView;
		ImageView imgImageView;
		
		TextView commentCountTextView;
		TextView lookCountTextView;
		TextView likeTextView;
		
		ImageButton replyButton;
		
		ImageView commentImage11, commentImage21, commentImage22,
		commentImage31, commentImage32, commentImage33;
		
        LinearLayout commentImageLayout, commentImageLayout1,
		commentImageLayout2, commentImageLayout3;
	}
}
